package com.fbee.smartcommunities.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: com.fbee.smartcommunities.utils
 * Datetime: 2024/8/16 下午4:32
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
public class ExpansionRequest extends HttpServletRequestWrapper {

    private Map<String,String[]> paramsMap;

    public ExpansionRequest(HttpServletRequest request) {
        super(request);
        paramsMap = new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        for(Map.Entry<String,String[]> entry : parameterMap.entrySet()){
            paramsMap.put(entry.getKey(),entry.getValue());
        }
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return this.paramsMap;
    }

    @Override
    public String getParameter(String name) {
        if(paramsMap.containsKey(name)){
            String[] values = paramsMap.get(name);
            return values.length > 0 ? values[0] : null;
        }
        return null;
    }

    @Override
    public String[] getParameterValues(String name) {
        if(paramsMap.containsKey(name)){
            return paramsMap.get(name);
        }
        return null;
    }

    public void addParameter (String name,Object value){
        if(paramsMap.containsKey(name)){
            String[] values = paramsMap.get(name);
            values = Arrays.copyOf(values, values.length + 1);
            values[values.length - 1] = String.valueOf(value);
        } else {
            paramsMap.put(name,new String[]{value.toString()});
        }
    }

}
